#!/usr/bin/perl
#use strict;
#use warnings;

my @triggers;
my @file;
use File::Copy;

open( TRIGGERFILE, "/Users/kimmasterson/trigger.txt" )
or die "Can't Open: $!\n";
@triggers = <TRIGGERFILE>;
print @triggers;

while (<TRIGGERFILE>)    #read each line into $_
{
    chomp @triggers;     # Remove newline from $_
    push @triggers, $_;  # add the line to @triggers
    #/puts all txt from trigger.txt into the array @triggers. /
}
close TRIGGERFILE;
#all grand works fine


########################################################################################

#open the directory with all stories
$dirtoget="/Users/kimmasterson/jennieProject/rssFolders/txt/";
opendir(IMD, $dirtoget) || die("Cannot open directory");
@thefiles= readdir(IMD);

foreach $f (@thefiles){
    if ($f =~ m/\.txt$/){
	#print "matches a txt file\n";
#print $f;
        open (FILE, "/Users/kimmasterson/jennieProject/rssFolders/txt/$f")or die"Cannot open FILE";
     
        if ( FILE eq "" ) {

            close FILE;
        }
        else{
       # print "In the Else\n";
            while (<FILE>) {
                foreach $word(split) {
                    foreach $x (@triggers) {
                       if  ($x =~ m/\b\Q$word\E\b/) {
                     
                   			print $word,"\n";
                   			    print $f,"\n";
                       
                            copy("/Users/kimmasterson/jennieProject/rssFolders/txt/$f","/Users/kimmasterson/crimeStories/$f")or die "Copy failed: $!";
                    close FILE;
                    } 
                    }

                }
            }
        }
    }
}
closedir(IMD);
exit 0;


